﻿using System;
using System.IO;
using Terraria.DataStructures;

namespace Terraria.Net
{
	// Token: 0x0200014F RID: 335
	public struct NetPacket
	{
		// Token: 0x17000116 RID: 278
		// (get) Token: 0x06000E8C RID: 3724 RVA: 0x0000E005 File Offset: 0x0000C205
		// (set) Token: 0x06000E8D RID: 3725 RVA: 0x0000E00D File Offset: 0x0000C20D
		public int Length { get; private set; }

		// Token: 0x17000117 RID: 279
		// (get) Token: 0x06000E8E RID: 3726 RVA: 0x0000E016 File Offset: 0x0000C216
		public BinaryWriter Writer
		{
			get
			{
				return this.Buffer.Writer;
			}
		}

		// Token: 0x17000118 RID: 280
		// (get) Token: 0x06000E8F RID: 3727 RVA: 0x0000E023 File Offset: 0x0000C223
		public BinaryReader Reader
		{
			get
			{
				return this.Buffer.Reader;
			}
		}

		// Token: 0x06000E90 RID: 3728 RVA: 0x003FFEA8 File Offset: 0x003FE0A8
		public NetPacket(ushort id, int size)
		{
			this = default(NetPacket);
			this.Id = id;
			this.Buffer = BufferPool.Request(size + 5);
			this.Length = size + 5;
			this.Writer.Write((ushort)(size + 5));
			this.Writer.Write(82);
			this.Writer.Write(id);
		}

		// Token: 0x06000E91 RID: 3729 RVA: 0x0000E030 File Offset: 0x0000C230
		public void Recycle()
		{
			this.Buffer.Recycle();
		}

		// Token: 0x06000E92 RID: 3730 RVA: 0x003FFF04 File Offset: 0x003FE104
		public void ShrinkToFit()
		{
			if (this.Length == (int)this.Writer.BaseStream.Position)
			{
				return;
			}
			this.Length = (int)this.Writer.BaseStream.Position;
			this.Writer.Seek(0, SeekOrigin.Begin);
			this.Writer.Write((ushort)this.Length);
			this.Writer.Seek(this.Length, SeekOrigin.Begin);
		}

		// Token: 0x04001248 RID: 4680
		private const int HEADER_SIZE = 5;

		// Token: 0x04001249 RID: 4681
		public readonly ushort Id;

		// Token: 0x0400124A RID: 4682
		public readonly CachedBuffer Buffer;
	}
}
